﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Collections.Generic;

namespace FYPHP
{
    public partial class CancelTransaction : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["fyphp"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {
            SqlCommand cmdRetrieveStatusAndShippingDetails, cmdDeleteTransaction,cmdDeleteShippingDetails;
            SqlDataReader dtrRAS;

            conn.Open();

            cmdRetrieveStatusAndShippingDetails = new SqlCommand("SELECT * FROM Transactions WHERE t_id=@TID",conn);
            cmdRetrieveStatusAndShippingDetails.Parameters.AddWithValue("@TID", Session["TransactionID"].ToString());

            dtrRAS = cmdRetrieveStatusAndShippingDetails.ExecuteReader();

            dtrRAS.Read();

            String t_status = dtrRAS["t_status"].ToString();
            String s_id = dtrRAS["s_id"].ToString();

            dtrRAS.Close();

            if (t_status.Equals("BuyNow"))
            {
                
            //    cmdDeleteShippingDetails = new SqlCommand("DELETE FROM Shipping_Details WHERE s_id=@SID",conn);
            //    cmdDeleteShippingDetails.Parameters.AddWithValue("@SID",s_id);

                cmdDeleteTransaction = new SqlCommand("DELETE FROM Transactions WHERE t_id=@TID",conn);
                cmdDeleteTransaction.Parameters.AddWithValue("@TID", Session["TransactionID"].ToString());
                cmdDeleteTransaction.ExecuteNonQuery();

                cmdDeleteTransaction = new SqlCommand("DELETE FROM Transactions_Products WHERE t_id=@TID", conn);
                cmdDeleteTransaction.Parameters.AddWithValue("@TID", Session["TransactionID"].ToString());
                cmdDeleteTransaction.ExecuteNonQuery();

            }
            else if (t_status.Equals("Cart"))
            {
           
            //    cmdDeleteShippingDetails = new SqlCommand("DELETE FROM Shipping_Details WHERE s_id=@SID", conn);
             //   cmdDeleteShippingDetails.Parameters.AddWithValue("@SID", s_id);

            }



            conn.Close();


            MessageBox("You Have Canceled Transaction");
            Redirect("Default.aspx");

        }




        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }

        private void Redirect(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language=\"javascript\">window.location='" + msg + "';</script>";
            Page.Controls.Add(lbl);
        }
    }
}